<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>添加教师-学生选课后台管理系统-WeAdmin 1.0</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../static/css/font.css">
		<link rel="stylesheet" href="../../static/css/weadmin.css">
		<script src="../../static/js/common.js" charset="utf-8"></script>
		<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
		<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
	</head>

	<body>
		<div class="weadmin-body">
			<form class="layui-form layui-form-pane">
				<div class="layui-form-item" pane>
					<label for="L_userno" class="layui-form-label">
		                <span class="we-red">*</span>教师工号
		            </label>
					<div class="layui-input-inline">
						<input type="text" id="L_userno" name="userno" lay-verify="required|nikename" autocomplete="off" class="layui-input">
					</div>
					<div class="layui-form-mid layui-word-aux">
						唯一的登录名
					</div>
				</div>

				<div class="layui-form-item" pane>
					<label for="L_username" class="layui-form-label">
						<span class="we-red">*</span>教师姓名
					</label>
					<div class="layui-input-inline">
						<input type="text" id="L_username" name="username" lay-verify="required|username" autocomplete="off" class="layui-input">
					</div>
				</div>

				<div class="layui-form-item" pane>
					<label for="L_birthday" class="layui-form-label">
						<span class="we-red">*</span>出生日期
					</label>
					<div class="layui-input-inline">
						<input type="text" id="L_birthday" name="birthday" lay-verify="required|date" autocomplete="off" placeholder="yyyy-MM-dd" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label for="L_age" class="layui-form-label">
						<span class="we-red">*</span>年龄
					</label>
					<div class="layui-input-inline">
						<input type="text" id="L_age" name="age" lay-verify="required|number|readonly" autocomplete="off"
							   class="layui-input" readonly>
					</div>
				</div>

				<div class="layui-form-item" pane>
					<label  class="layui-form-label">
						<span for="L_department" class="we-red">*</span>隶属学院
					</label>
					<div class="layui-input-inline">
						<select id="L_department" name="department" lay-verify="required" lay-filter="department" >

						</select>
					</div>
				</div>

				<div class="layui-form-item" pane>
					<label  class="layui-form-label">
						<span for="L_gradeclass" class="we-red">*</span>专业
					</label>
					<div class="layui-input-inline">
						<select id="L_gradeclass" name="gradeclass" lay-verify="required" lay-search>

						</select>
					</div>
				</div>

				<div class="layui-form-item" pane>
				    <label for="L_sex" class="layui-form-label">性别</label>
				    <div class="layui-input-block" id="L_sex">
				      <input type="radio" name="sex" value="1" title="男" checked>
				      <input type="radio" name="sex" value="2" title="女">
				      <input type="radio" name="sex" value="3" title="未知">
				    </div>
				</div>

				<div class="layui-form-item" pane>
					<label for="L_email" class="layui-form-label">
		                <span class="we-red">*</span>手机号码
		            </label>
					<div class="layui-input-inline">
						<input type="text" id="L_phone" name="phone" lay-verify="required|phone" autocomplete="" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item" pane>
					<label for="L_email" class="layui-form-label">
		                <span class="we-red">*</span>邮箱
		            </label>
					<div class="layui-input-inline">
						<input type="text" id="L_email" name="email" autocomplete="off" class="layui-input">
					</div>

				</div>

				<div class="layui-form-item" pane>
					<label class="layui-form-label"><span class="we-red">*</span>身份证号码</label>
					<div class="layui-input-inline">
						<input type="text"id="L_identity" name="identity" lay-verify="identity|required" placeholder="" autocomplete="off" class="layui-input">
					</div>
					<div class="layui-form-mid layui-word-aux">
						初始密码为身份证号后6位
					</div>
				</div>

				<div class="layui-form-item layui-hide" pane>
					<label for="L_pass" class="layui-form-label">
                  <span class="we-red">*</span>密码
              </label>
					<div class="layui-input-inline">
						<input type="password" id="L_pass" name="pass" lay-verify="required|pass|readonly" autocomplete="off" class="layui-input" readonly>
					</div>
					<div class="layui-form-mid layui-word-aux">
						初始密码为身份证号后6位
					</div>
				</div>

				<div class="layui-form-item">
					<label  class="layui-form-label">
					</label>
					<button class="layui-btn" lay-filter="add" lay-submit="">确定</button>
				</div>
			</form>
		</div>
		<script src="../../lib/layui/layui.js" charset="utf-8"></script>

		<script type="text/javascript">

			layui.extend({
				admin: '{/}../../static/js/admin'
			});



			layui.use(['form', 'jquery','util','admin', 'layer','laydate'], function() {
                var form = layui.form,
                    $ = layui.jquery,
                    util = layui.util,
                    admin = layui.admin,
                    layer = layui.layer;
                	laydate = layui.laydate;

                //日期
                laydate.render({
                    elem: '#L_birthday',
					done:function(value,date,endDate){
                      var curYear =  new Date().toLocaleDateString().split("/")[0];
                      var chooseYear = date.year;
                      var age = curYear-chooseYear;
                      $('input[name="age"]').val(age);
                    }
                });
				//加载部门信息
                $.ajax({
                    url: remoteUrl+"department",
                    type: 'get',//method请求方式，get或者post
                    dataType: 'json',//预期服务器返回的数据类型
                    contentType: "application/json; charset=utf-8",
                    async: false,
                    success: function (res) {//res为相应体,function为回调函数
                        var result = res.result;
                        var i = 0;
                        var op = "";
                        $('#L_department').append('<option value="">请选择</option>');
                        for (i; i < result.length; i++) {
                            op += '<option value=' + result[i].id + '>' + result[i].name + '</option>';
                        }
                        $('#L_department').append(op);
                        form.render();
                    }
                });
                form.on('select(department)',function (data) {
                    var value = data.value;
                    if(value == "" ){
                        $('#L_gradeclass').html("");
                        $('#L_gradeclass').append('<option value="">请选择</option>');
                        form.render();
					}else
						$.ajax({
							url: remoteUrl+"grade_class/getbyDepartment/"+data.value,
							type: 'get',//method请求方式，get或者post
							dataType: 'json',//预期服务器返回的数据类型
							contentType: "application/json; charset=utf-8",
							async: false,
							success: function (res) {//res为相应体,function为回调函数
								var result = res.result;
								var i = 0;
								var op = "";
								$('#L_gradeclass').html("");
								for (i; i < result.length; i++) {
									op += '<option value=' + result[i].id + '>' + result[i].name + '</option>';
								}
								$('#L_gradeclass').append(op);
								form.render();
							}
						});
                });


				//自定义验证规则
				form.verify({
					nikename: function(value) {
						if(value.length < 5) {
							return '昵称至少得5个字符啊';
						}
					},
					pass: [/(.+){6,12}$/, '密码必须6到12位'],
				});

                form.verify({
                    username: function(value) {
                        if(value.length < 2) {
                            return '昵称至少得2个字符啊';
                        }
                    },
                });
				//失去焦点时判断值为空不验证，一旦填写必须验证
				$('input[name="email"]').blur(function(){
					//这里是失去焦点时的事件
					if($('input[name="email"]').val()){
						$('input[name="email"]').attr('lay-verify','email');
					}else{
						$('input[name="email"]').removeAttr('lay-verify');
					}
				});

                //截取身份证号码后六位作为密码
                $('input[name="identity"]').blur(function(){
                    //这里是失去焦点时的事件
                    $('input[name="pass"]').val($('input[name="identity"]').val().substr(12,6));
                });



				//监听提交
				form.on('submit(add)', function(data) {
				    var data = {
                        "user":
                            {
                                "number": $('#L_userno').val(),
                                "username": $('#L_username').val(),
                                "password":  $('#L_pass').val(),
								"roles":[
								    	{
								    	    "id": 3//教师角色ID
										}
								    ],
								"status": 1//设置为生效状态
                            },
						"birthday":$('#L_birthday').val(),
                        "age":$('#L_age').val(),
                        "sex": $("input[name='sex']:checked").val(),
                        "phoneNumber": $('#L_phone').val(),
                        "email": $('#L_email').val(),
                        'idNumber': $('#L_identity').val(),
                        "department": {
                            "id": $("#L_department").find("option:selected").val()
                        },
                        "gradeClass": {
                            "id": $("#L_gradeclass").find("option:selected").val()
                        }
					};


                    $.ajax({
                        url: remoteUrl+"teacher",
                        type: 'post',//method请求方式，get或者post
                        dataType: 'json',//预期服务器返回的数据类型
                        data: JSON.stringify(data),
                        contentType: "application/json; charset=utf-8",
                        success: function (res) {//res为相应体,function为回调函数
                            if (res.status === 200) {
                                layer.alert("增加成功", {
                                    icon: 6
                                }, function() {
                                    // 获得frame索引
                                    var index = parent.layer.getFrameIndex(window.name);
                                    //关闭当前frame
                                    parent.layer.close(index);
                                });
                            } else {
                                layer.msg(res.message);
                            }
                        }
                    });
				    return false;
				});

			});
		</script>
	</body>

</html>